package br.com.infocruz.seam.crm.ejb.jpa;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.hibernate.ejb.QueryImpl;

public class JpaQueryHelper {
	public static <T> String getDefaultEjbql(Class<T> entityClass) {
		return new StringBuilder("select object(").append(getEntityAlias(entityClass)).append(") from ").append(entityClass.getName()).append(" as ").append(getEntityAlias(entityClass)).toString();
	}

	public static <T> String getEntityAlias(Class<T> entityClass) {
		return StringUtils.uncapitalize(entityClass.getSimpleName());
	}

	public static String getQueryString(Query query) {
		QueryImpl queryImpl = (QueryImpl) query;
		return queryImpl.getHibernateQuery().getQueryString();
	}
}
